package com.android.phoenix.activity;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.phoenix.Constant;
import com.android.phoenix.MainApplication;
import com.android.phoenix.R;

/**
 * @ClassName: RegisterActivity
 * @Description: 用户注册类
 * @author: wanchao
 * @date: 2014-12-27 上午11:02:46
 */
public class RegisterActivity extends BaseActivity implements OnClickListener {
	private EditText editRegisterPhone = null;
	private EditText editRegisterPassword = null;
	private ImageView imgRegisterBack = null;
	private ImageView imgRegisterDeletePhone = null;
	private ImageView imgRegisterDeletePassword = null;
	private Button btnRegister = null;

	private String txtEditPhone = "";
	private String txtEditPassword = "";

	/** 验证手机号格式是否正确的正则表达式 */
	Pattern patPhone = Pattern
			.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");
	Pattern patPassWord = Pattern.compile("^[0-9a-zA-Z]{6,16}$");

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.acti_register);

		initViews();
	}

	/**
	 * @Title: initViews
	 * @Description: 控件等初始化
	 * @author: wanchao
	 * @date: 2014-12-26 下午5:18:52
	 */
	private void initViews() {
		editRegisterPhone = (EditText) findViewById(R.id.editRegisterPhone);
		editRegisterPassword = (EditText) findViewById(R.id.editRegisterPassword);
		imgRegisterBack = (ImageView) findViewById(R.id.imgRegisterBack);
		imgRegisterDeletePhone = (ImageView) findViewById(R.id.imgRegisterDeletePhone);
		imgRegisterDeletePassword = (ImageView) findViewById(R.id.imgRegisterDeletePassword);
		btnRegister = (Button) findViewById(R.id.btnRegister);

		editRegisterPhone.addTextChangedListener(textWatcherPhone);
		editRegisterPassword.addTextChangedListener(textWatcherPassword);
		imgRegisterBack.setOnClickListener(this);
		imgRegisterDeletePhone.setOnClickListener(this);
		imgRegisterDeletePassword.setOnClickListener(this);
		btnRegister.setOnClickListener(this);
		btnRegister.setEnabled(false);
	}

	// editRegisterPhone内容改变监听
	private TextWatcher textWatcherPhone = new TextWatcher() {
		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			if (!s.toString().equals("")) {
				txtEditPhone = s.toString();
				imgRegisterDeletePhone.setVisibility(View.VISIBLE);
				if (!TextUtils.isEmpty(txtEditPassword)) {
					btnRegister.setEnabled(true);
				}
			} else {
				btnRegister.setEnabled(false);
				txtEditPhone = s.toString();
				imgRegisterDeletePhone.setVisibility(View.INVISIBLE);
			}
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}

		@Override
		public void afterTextChanged(Editable s) {
		}
	};
	// editRegisterPassword内容改变监听
	private TextWatcher textWatcherPassword = new TextWatcher() {
		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			if (!s.toString().equals("")) {
				txtEditPassword = s.toString();
				imgRegisterDeletePassword.setVisibility(View.VISIBLE);
				if (!TextUtils.isEmpty(txtEditPhone)) {
					btnRegister.setEnabled(true);
				}
			} else {
				btnRegister.setEnabled(false);
				txtEditPassword = s.toString();
				imgRegisterDeletePassword.setVisibility(View.INVISIBLE);
			}
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}

		@Override
		public void afterTextChanged(Editable s) {
		}
	};

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int id = v.getId();
		switch (id) {
		case R.id.imgRegisterBack:
			finish();
			break;
		case R.id.imgRegisterDeletePhone:
			editRegisterPhone.setText("");
			break;
		case R.id.imgRegisterDeletePassword:
			editRegisterPassword.setText("");
			break;
		case R.id.btnRegister:
			register();
			break;
		}
	}

	/**
	 * @Title: register
	 * @Description: 验证输入信息，存储帐号和密码
	 * @author: WanChao
	 * @date: 2015-1-23 下午10:15:10
	 */
	private void register() {
		final String phoneNum = editRegisterPhone.getText().toString();
		final String passWord = editRegisterPassword.getText().toString();
		Matcher matPhone = patPhone.matcher(phoneNum);
		Matcher matPassWord = patPassWord.matcher(passWord);
		if (!matPhone.matches()) {
			showMyDialog(this, "提示", "您输入的手机号码不正确！");
			return;
		} else if (!matPassWord.matches()) {
			showMyDialog(this, "提示", "密码为6~16位的数字或字母！");
			return;
		}

		Editor edit = MainApplication.mSharedPreferences.edit();
		edit.putString(Constant.PASSWORD, editRegisterPassword.getText()
				.toString().trim());
		edit.putString(Constant.USERNAME, editRegisterPhone.getText()
				.toString().trim());
		edit.commit();
		Toast.makeText(RegisterActivity.this,
				getResources().getString(R.string.register_success),
				Toast.LENGTH_SHORT).show();
		finish();
	}

	/**
	 * @Title: showDialog
	 * @Description: 弹出对话框
	 * @author: WanChao
	 * @date: 2015-1-23 下午10:35:07
	 * @param context
	 * @param title
	 * @param message
	 */
	public static void showMyDialog(Context context, String title,
			String message) {
		AlertDialog.Builder dialog = new Builder(context);
		dialog.setTitle(title);
		dialog.setMessage(message);
		dialog.setPositiveButton("确认",
				new android.content.DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		dialog.create().show();
	}

}
